<template>
  <el-dialog
    v-model="visible"
    title="批量确认资产"
    width="600px"
    :close-on-click-modal="false"
  >
    <div class="batch-confirm-content">
      <div class="info-section">
        <p>
          已选择 <strong>{{ selectedIds.length }}</strong> 个资产进行批量确认
        </p>
      </div>

      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="责任部门" prop="confirmed_department">
          <el-select
            v-model="form.confirmed_department"
            placeholder="请选择责任部门（可选）"
            clearable
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="dept in props.departmentOptions || []"
              :key="dept.value"
              :label="dept.label"
              :value="dept.value"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="责任人" prop="confirmed_person">
          <el-input
            v-model="form.confirmed_person"
            placeholder="请输入责任人（可选）"
          />
        </el-form-item>
        <el-form-item label="确认原因" prop="confirm_reason">
          <el-input
            v-model="form.confirm_reason"
            type="textarea"
            :rows="4"
            placeholder="请输入确认原因（可选）"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="submitting" @click="handleSubmit">
        确认
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from "vue";
import { ElMessage, ElLoading } from "element-plus";
import { batchConfirmPanAssets } from "../../../servers";

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  selectedIds: { type: Array as () => number[], default: () => [] },
  departmentOptions: {
    type: Array as () => Array<{ label: string; value: string }>,
    default: () => [],
  },
});

const emit = defineEmits<{
  (e: "update:modelValue", value: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit("update:modelValue", value),
});

const formRef = ref();
const submitting = ref(false);
const form = ref({
  confirmed_department: "",
  confirmed_person: "",
  confirm_reason: "",
});

const rules = {
  // 所有字段都是可选的，不需要必填验证
};

// 监听弹窗打开，初始化表单
watch(visible, (newVal) => {
  if (newVal) {
    form.value.confirmed_department = "";
    form.value.confirmed_person = "";
    form.value.confirm_reason = "";
  }
});

// 提交
const handleSubmit = async () => {
  if (props.selectedIds.length === 0) {
    ElMessage.warning("请先选择要确认的资产");
    return;
  }

  submitting.value = true;
  try {
    const params: any = {
      asset_ids: props.selectedIds,
    };

    if (form.value.confirmed_department) {
      params.confirmed_department = form.value.confirmed_department;
    }
    if (form.value.confirmed_person) {
      params.confirmed_person = form.value.confirmed_person;
    }
    if (form.value.confirm_reason) {
      params.confirm_reason = form.value.confirm_reason;
    }

    const result = await batchConfirmPanAssets(params);

    const confirmedCount = result.data.confirmed_count || 0;
    const totalCount = result.data.total_count || 0;
    ElMessage.success(`成功确认 ${confirmedCount}/${totalCount} 个资产`);
    emit("success");
    emit("update:modelValue", false);
  } catch (error: any) {
    ElMessage.error(error?.message || "批量确认失败");
  } finally {
    submitting.value = false;
  }
};

// 取消
const handleCancel = () => {
  emit("update:modelValue", false);
};
</script>

<style scoped lang="scss">
.batch-confirm-content {
  .info-section {
    margin-bottom: 20px;
    padding: 15px;
    background-color: #f5f7fa;
    border-radius: 4px;

    p {
      margin: 0;
      color: #606266;

      strong {
        color: #409eff;
        font-size: 16px;
      }
    }
  }
}
</style>
